package com.cff.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.apache.commons.lang3.StringUtils;

import com.cff.common.Constant;
import com.cff.common.SimpleFrame;
import com.cff.util.DateUtil;
import com.cff.util.EventUtil;
import com.cff.util.FileUtil;
import com.cff.util.SignTools;
import com.cff.util.TelnetTools;
import com.cff.*;

public class PointGrantActionListener implements ActionListener {
	private static Properties confPro = null;
	
	public PointGrantActionListener() {
		super();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		confPro = FileUtil.getInstance();
		final JFrame tmpFrame = new JFrame();
		tmpFrame.setBounds(100, 100, 600, 400);
		tmpFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		tmpFrame.setLayout(null);
		tmpFrame.setTitle(confPro.getProperty("tran1title"));
		
		JLabel outUserNoLabel = new JLabel();
		outUserNoLabel.setBounds(20, 20, 80, 50);
		outUserNoLabel.setText(confPro.getProperty("tran1param1"));
		
		JLabel inUserNoLabel = new JLabel();
		inUserNoLabel.setBounds(20, 70, 80, 50);
		inUserNoLabel.setText(confPro.getProperty("tran1param2"));
		
		JLabel actIdLabel = new JLabel();
		actIdLabel.setBounds(20, 120, 80, 50);
		actIdLabel.setText(confPro.getProperty("tran1param3"));
		
		JLabel pointAmtLabel = new JLabel();
		pointAmtLabel.setBounds(20, 170, 80, 50);
		pointAmtLabel.setText(confPro.getProperty("tran1param4"));
		
		JLabel resultLabel = new JLabel();
		resultLabel.setBounds(20, 240, 80, 50);
		resultLabel.setText(confPro.getProperty("tran1result"));
		
		final JTextField outUserNoText = new JTextField();
		outUserNoText.setBounds(100, 35, 180, 20);
		outUserNoText.setText(confPro.getProperty("tran1param1desc"));
		
		final JTextField inUserNoText = new JTextField();
		inUserNoText.setBounds(100, 85, 180, 20);
		
		final JTextField actIdText = new JTextField();
		actIdText.setBounds(100, 135, 180, 20);
		actIdText.setText(confPro.getProperty("tran1param2desc"));
		
		final JTextField pointAmtText = new JTextField();
		pointAmtText.setBounds(100, 185, 180, 20);
		
		final JTextField resultText = new JTextField();
		resultText.setBounds(100, 255, 180, 20);
	
		JButton excuteButton = new JButton("执行"); 
		excuteButton.setBounds(150, 300, 100, 30);
        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(300, 300, 100, 30);
        
		tmpFrame.add(actIdLabel);
		tmpFrame.add(inUserNoLabel);
		tmpFrame.add(outUserNoLabel);
		tmpFrame.add(resultLabel);
		tmpFrame.add(pointAmtLabel);
		tmpFrame.add(outUserNoText);
		tmpFrame.add(inUserNoText);
		tmpFrame.add(resultText);
		tmpFrame.add(pointAmtText);
		tmpFrame.add(actIdText);
		tmpFrame.add(excuteButton);
		tmpFrame.add(cancelButton);
		
		excuteButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				String outUserNo = outUserNoText.getText().trim();
				String inUserNo = inUserNoText.getText().trim();
				String actId = actIdText.getText().trim();
				String pointAmt = pointAmtText.getText().trim();
				if(StringUtils.isEmpty(outUserNo) || StringUtils.isEmpty(inUserNo) || StringUtils.isEmpty(actId) || StringUtils.isEmpty(pointAmt)){
					JOptionPane.showMessageDialog(null,"数据输入为空！");
					return;
				}
				String nowDate = DateUtil.format(new Date(), "yyyyMMdd");
				String data = "101210|" + 
						nowDate +"|"+ nowDate +
						"|154059|1471592459376|004|T02100002|"+
						outUserNo + "|" + inUserNo + "|"+ actId + "|" + pointAmt+"|0||02|77777777|havemoney";
				TelnetTools telnetTools = new TelnetTools();
				if("".equals(AwtPos.hsmIp)){
					telnetTools.setHsmIp(confPro.getProperty("hsmip"));
				}else{
					telnetTools.setHsmIp(AwtPos.hsmIp);
				}
				if("".equals(AwtPos.hsmPort)){
					telnetTools.setHsmPort(confPro.getProperty("hsmport"));
				}else{
					telnetTools.setHsmPort(AwtPos.hsmPort);
				}
				if("".equals(AwtPos.transIp)){
					telnetTools.setDestIp(confPro.getProperty("app11ip"));
				}else{
					telnetTools.setDestIp(AwtPos.transIp);
				}
				if("".equals(AwtPos.transPort)){
					telnetTools.setDestPort(confPro.getProperty("app11port"));
				}else{
					telnetTools.setDestPort(AwtPos.transPort);
				}
				String result = telnetTools.trans(data, telnetTools);
				String results[] = result.split("\\|"); 
				resultText.setText(results[10]);
			}

		});
		
		cancelButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				tmpFrame.dispose();
			}
			
		});
		
		tmpFrame.setVisible(true);
		
	}

}
